<?php
/**
 * 
 * 用户管理模块
 * @author 
 * @version v1.0.0
 *
 **/
namespace Mama\Controller;
use Think\Controller;

class MemberController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(!session('userid')){ //已登陆
			$this->redirect("index/index");
		}
		//解析分店信息
		$shoplist = S('shoplist_'. session('userid'));
		$this->assign('shoplist', $shoplist);
		if(count($shoplist)) foreach($shoplist as $sp){
			$this->shopidlist .= strlen($this->shopidlist) ? ','. $sp['shop_id'] : $sp['shop_id'];
		}
		//传递参数到分页界面
		$this->assign('param', json_encode(I('get.')));
    }
	
	/**
	 * 编辑管理员信息
	 **/
	public function admin_add(){
		if(IS_GET){
			//获取角色列表
			$rolelist = D('Role')->getRoleList();
			$this->assign('rolelist', $rolelist);
			$this->assign('shoplist', S('shoplist_'. session('userid')));
			//获取用户详情
			$userid = I('get.userid', 0);
			if($userid){
				$userinfo = D('Member')->getMemberInfoByUserid($userid);
				$this->assign('userinfo', $userinfo);
				//获取管理分店列表
				$shoplist = D('Shop')->getShoprofileList($userinfo['userid']);
				foreach($shoplist as $ss){
					$shopidlist .= strlen($shopidlist) ? ','. $ss['shop_id'] : $ss['shop_id'];
				}

				$this->assign('shopidlist', ','. $shopidlist . ',');
			}

			$this->display();
		}else{
			//保存数据
			$data = I('post.');
			$memberDao = D('Member');
			$shop_list = $data['shop_id'];
			unset($data['shop_id']);
			$data['password'] = md5($data['password']);
			//追加固定属性
			$data['admin_id'] = session('userid');
			$data['admin_nick'] = session('nickname');
			$data['createtime'] = time();
			//添加关系
			$shopDao = D('Shop');
			$userid = $data['userid'];
			if($userid){
				unset($data['userid']);
				$row = $memberDao->editMemberInfo($userid, $data);
				if($row !== false){
					//删除分店关系
					$shopDao->deleteAllProfile($userid);
				}
			}else{
				unset($data['userid']);
				$row = $memberDao->registerMemberInfo($data);
				$userid = $row;
			}
			
			foreach($shop_list as $sp){
				$shoplist[] = array(
					'userid' => $userid,
					'shop_id' => $sp,
					'createtime' => time()
				);
			}

			if($row){
				//添加分店信息
				if(count($shoplist)){
					$shopDao->addAllShopprofileInfo($shoplist);
				}
				echo returnMessage(1, '编辑成功');
			}else{
				echo returnMessage(0, '编辑失败');
			}
		}
	}

	/**
	 * 获取管理员列表
	 **/
	public function adminlist(){
		$ajax = I('get.ajax', 0);
		$page = I('get.page', 1);
		//查询管理员
		$where['roleid'] = array('gt', 0);
		//获取条件
		$shopid = I('get.shopid', 0);
		if($shopid){
			//查询userid集合
			$userArr = D('Shop')->getUserListByShopId($shopid);
			if(count($userArr)){
				foreach($userArr as $us){
					$userAttr[] = $us['userid'];
				}
			}

			if(count($userAttr)){
				$where['userid'] = array('in', $userAttr);
			}
		}
		//获取姓名
		$nickname = I('get.nickname', '');
		if(!empty($nickname)){
			$where['nickname'] = array('like', '%'. $nickname .'%');
		}
		//获取用户状态
		$visible = I('get.visible');
		if($visible != 'all' && !isset($visible)){
			$where['visible'] = $visible;
		}
		//获取总数
		$memberDao = D('Member');
		$count = $memberDao->getAdminMemberCount($where);
		$this->assign('count', $count);
		//获取列表
		$userlist = $memberDao->getAdminMemberList($where, $page);
		//echo $memberDao->_sql();
		$this->assign('userlist', $userlist);
		//判断管理员管理的分店列表
		$this->assign('shoplist', S('shoplist_'. session('userid')));
		if($ajax){
			$this->display('adminlist_list', 'utf-8', 'html');
		}else{
			$this->display();
		}
	}

	/**
	 * 会员列表
	 **/
	public function lists(){
		$ajax = I('get.ajax', 0);
		$page = I('get.page', 1);
		//查询管理员
		$where['roleid'] = array('lt', 1);
		//获取条件
		$shopid = I('get.shopid', 0);
		if($shopid){
			//查询userid集合
			$where['shop_id'] = $shopid;
		}
		//获取姓名
		$nickname = I('get.nickname', '');
		if(!empty($nickname)){
			$where['nickname'] = array('like', '%'. $nickname .'%');
		}
		//获取用户状态
		$visible = I('get.visible');
		if($visible){
			$where['visible'] = $visible-1;
		}
		//获取总数
		$memberDao = D('Member');
		$count = $memberDao->getMemberCount($where);
		$this->assign('count', $count);
		//获取列表
		$userlist = $memberDao->getMemberList($where, $page);
		//echo $memberDao->_sql();
		$this->assign('userlist', $userlist);
		//判断管理员管理的分店列表
		$this->assign('shoplist', S('shoplist_'. session('userid')));
		if($ajax){
			$this->display('list_table', 'utf-8', 'html');
		}else{
			$this->display();
		}
	}

	/**
	 * 修改状态
	 **/
	public function setStatus(){
		if(IS_POST){
			$userid = I('post.userid', 0);
			$visible = I('post.visible', 0);

			$row = D('Member')->editMemberInfo($userid, array(
				'visible' => $visible
			));

			if($row !== false){
				echo 1;
			}else{
				echo 0;
			}
		}
	}
	
	/**
	 * 合同管理
	 **/
	public function contract(){
		//获取缓存
		$shoplist = S('shoplist_'. session('userid'));
		//判断管理的分店列表
		foreach($shoplist as $sp){
			$shop .= strlen($shop) ? ','. $sp['shop_id'] : $sp['shop_id'];
		}

		$ajax = I('get.ajax', 0);
		$page = I('get.page', 1);
		//查询管理员
		$where['member.roleid'] = array('lt', 1);
		//获取条件
		$shopid = I('get.shopid', 0);
		if($shopid){
			//查询userid集合
			$where['pros.shop_id'] = $shopid;
		}else{
			$where['pros.shop_id'] = array('in', $shop);
		}
		//获取姓名
		$nickname = I('get.nickname', '');
		if(!empty($nickname)){
			$where['member.nickname'] = array('like', '%'. $nickname .'%');
		}
		$doornumber = I('get.doornumber', '');
		if(!empty($doornumber)){
			$where['pros.doornumber'] = $doornumber;
		}
		$contract = I('get.contract', '');
		if(!empty($contract)){
			$where['pros.contract'] = $contract;
		}
		//获取用户状态
		$visible = I('get.visible', 0);
		if($visible){
			$where['pros.visible'] = $visible;
		}
		//获取总数
		$memberDao = D('Member');
		$count = $memberDao->getContractMemberCount($where);
		$this->assign('count', $count);
		//获取列表
		$userlist = $memberDao->getContractMemberList($where, $page);
		//echo $memberDao->_sql();
		$this->assign('userlist', $userlist);
		//判断管理员管理的分店列表
		$this->assign('shoplist', $shoplist);
		if($ajax){
			$this->display('contract_list', 'utf-8', 'html');
		}else{
			$this->display();
		}
	}

	/**
	 * 合同详情
	 **/
	public function contractinfo(){
		if(IS_GET){
			$profile_id = I('get.profile_id', 0);
			//获取配套信息
			$accesslist = D('Access')->getAllAccessList(array(
				'visible' => 1
			));
			//获取详情
			if($profile_id){
				$info = D('Member')->getContractMemberInfo($profile_id);
				//获取小区列表
				$villagelist = D('Village')->getVillageListByShopId(array(
					'shop_id' => $info['shop_id']
				));
				$this->assign('villagelist', $villagelist);
				$this->assign('userinfo', $info);
				//获取房间的标配信息
				$roominfo = D('Room')->getRoomInfoByWhere(array(
					'room_id' => $info['roomid']
				));
				//遍历合同的减配信息
				$room_matching = $roominfo['matching'];
				$room_matching_attr = explode(',', $room_matching);
				//合同减配信息
				$contract_matching = $info['matching'];
				$contract_matching_attr = explode(',', $contract_matching);
				//合并数组去重
				$matching = array_unique(array_merge($room_matching_attr, $contract_matching_attr));
				foreach($accesslist as &$ls){
					if(in_array($ls['access_id'], $matching)){
						$ls['selected'] = 1;
					}else{
						$ls['selected'] = 0;
					}
				}
			}
			
			$this->assign('accesslist', $accesslist);
			$this->display();
		}else if(IS_POST){
			$data = I('post.');
			//判断是否存在用户
			$memberDao = D('Member');
			$uinfo = $memberDao->getMemberInfoByUsername($data['phone']);
			if(count($uinfo)){
				$userid = $uinfo['userid'];
			}else{
				//注册新用户
				$userid = $memberDao->registerMemberInfo(array(
					'username' => $data['phone'],
					'roleid' => 0,
					'password' => md5('123456'),
					'nickname' => $data['nickname'],
					'card' => $data['card'],
					'phone' => $data['phone'],
					'address' => $data['address'],
					'emergency_name' => $data['emergency_name'],
					'emergency_phone' => $data['emergency_phone'],
					'login_type' => 1,
					'createtime' => time()
				));
			}
			//编辑合同信息
		}
	}

	/**
	 * 会员录入
	 **/
	public function add(){
		if(IS_GET){
			$userid = I('get.userid', 0);
			$userinfo = D('Member')->getMemberInfoByUserid($userid);
			$this->assign('userinfo', $userinfo);
			$this->display();
		}else if(IS_POST){

		}
	}

	/**
	 * 续约提醒
	 **/
	public function renew(){
		$memberDao = D('Member');
		if(IS_GET){
			//获取缓存
			$shoplist = S('shoplist_'. session('userid'));
			//判断管理的分店列表
			foreach($shoplist as $sp){
				$shop .= strlen($shop) ? ','. $sp['shop_id'] : $sp['shop_id'];
			}

			$ajax = I('get.ajax', 0);
			$page = I('get.page', 1);
			//查询管理员
			$where['member.roleid'] = array('lt', 1);
			//获取条件
			$shopid = I('get.shopid', 0);
			if($shopid){
				//查询userid集合
				$where['pros.shop_id'] = $shopid;
			}else{
				$where['pros.shop_id'] = array('in', $shop);
			}
			
			$contract = I('get.contract', '');
			if(!empty($contract)){
				$where['pros.contract'] = $contract;
			}
			//获取查询时间
			$bdays = date('Y-m-d', strtotime("-1 day"));
			$btime = I('get.btime', $bdays);
			$etime = I('get.etime', date('Y-m-d', time()));
			//设置查询条件
			$where['pros.signing_etime'] = array('between',array(strtotime($btime. ' 00:00:00'),strtotime($etime. ' 23:59:59')));
			//获取总数
			$count = $memberDao->getContractMemberCount($where);
			$this->assign('count', $count);
			//获取列表
			$userlist = $memberDao->getContractMemberList($where, $page);
			//echo $memberDao->_sql();
			$this->assign('userlist', $userlist);
			//判断管理员管理的分店列表
			$this->assign('shoplist', $shoplist);
			//传递时间参数
			$this->assign('btime', $btime);
			$this->assign('etime', $etime);
			
			if($ajax){
				$this->display('contract_list', 'utf-8', 'html');
			}else{
				$this->display();
			}
		}else if(IS_POST){
			$userid = I('post.userid', 0);
			$userinfo = $memberDao->getContractMemberInfo($userid);
			$type = I('type', 0);
			$rewresult = I('post.rewresult', 0);
			if($userinfo){
				if($type){
					$row = $memberDao->editContractInfoByProfileId($userid, array(
						'rewresult' => $rewresult
					));
				}else{
					$flag = sendSMSMessage($userinfo['phone'], '', $userinfo['nickname'], '',$userinfo['doornumber'], date('Y-m-d', $userinfo['signing_etime']), 2);
					if($flag){
						$row = $memberDao->editContractInfoByProfileId($userid, array(
							'rewentime' => time(),
							'rewencount' => array('exp', 'rewencount + 1')
						));
						/*
						//添加操作日志
						M('admin_log')->data(array(
							'type' => 1,
							'log_id' => $userid,
							'createtime' => time(),
							'admin_nick' => session('nickname')
						))->add();*/
					}
				}
				
				if($row !== false){
					echo 1;
				}else{
					echo 0;
				}
			}else{
				echo 0;
			}
		}	
	}
}


   